package com.zyy.api.converter;

import com.zyy.api.constant.LabelType;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *@author zhangyangyang
 *@createTime 2023414 15:54
 *@description 标签类型转换器
 */
@Converter
public class LabelTypeConverter implements AttributeConverter<List<LabelType>, String> {

    @Override public String convertToDatabaseColumn(List<LabelType> labelTypes) {
        return labelTypes.stream().map(LabelType::getType).map(type->String.valueOf(type)).collect(Collectors.joining(","));
    }

    @Override public List<LabelType> convertToEntityAttribute(String s) {
        return Arrays.stream(s.split(",")).map(type->LabelType.convert(Integer.valueOf(type))).collect(Collectors.toList());
    }
}
